package com.example.demo.controller;

import com.example.demo.rabbitmq.commonDelay.DelayCommonMessageSender;
import com.example.demo.rabbitmq.deadletter.BusinessMessageSender;
import com.example.demo.rabbitmq.delay.DelayMessageSender;
import com.example.demo.rabbitmq.delay.DelayTypeEnum;
import com.example.demo.rabbitmq.improveCommonDelay.DelayImproveCommonMessageSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RequestMapping("rabbitmq")
@RestController
public class RabbitMQMsgController {

    @Autowired
    private BusinessMessageSender sender;

    @Autowired
    DelayMessageSender delayMessageSender;

    @Autowired
    DelayCommonMessageSender commonMessageSender;

    @Autowired
    DelayImproveCommonMessageSender improveCommonMessageSender;


    /**
     * 正常消息：http://localhost:8089/rabbitmq/sendDeadlettermsg?msg=msg
     * 死信消息：http://localhost:8089/rabbitmq/sendDeadlettermsg?msg=deadletter
     *
     * @param msg
     */
    @RequestMapping("sendDeadlettermsg")
    public void sendMsg(String msg) {
        sender.sendMsg(msg);
    }

    /**
     * 延时10秒：http://localhost:8089/rabbitmq/sendDelaymsg?msg=testMsg1&delayType=1
     * 延时60秒：http://localhost:8089/rabbitmq/sendDelaymsg?msg=testMsg2&delayType=2
     *
     * @param msg
     * @param delayType
     */
    @RequestMapping("sendDelaymsg")
    public void sendDelayMsg(String msg, int delayType) {
        log.info("当前时间：{},收到请求，msg:{},delayType:{}", new Date(), msg, delayType);
        DelayTypeEnum delayTypeEnum = DelayTypeEnum.getDelayTypeEnumByValue(delayType);
        delayMessageSender.sendMsg(msg, delayTypeEnum);
    }

    /**
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendCommonDelaymsg?msg=testMsg3&ttlTime=50000
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendCommonDelaymsg?msg=testMsg3&ttlTime=20000
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendCommonDelaymsg?msg=testMsg3&ttlTime=2000
     *
     * @param msg
     * @param ttlTime
     */
    @RequestMapping("sendCommonDelaymsg")
    public void sendCommonMsg(String msg, String ttlTime) {
        log.info("当前时间：{},收到请求，msg:{},ttlTime:{}", new Date(), msg, ttlTime);
        MessageProperties props = new MessageProperties();
        props.setExpiration(ttlTime);
        Message toSendMsg = new Message(msg.getBytes(), props);
        commonMessageSender.sendMsg(toSendMsg);
    }

    /**
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendImporveCommonDelaymsg?msg=testMsg4&delayTime=50000
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendImporveCommonDelaymsg?msg=testMsg44&delayTime=20000
     * 自定义延时时间：http://localhost:8089/rabbitmq/sendImporveCommonDelaymsg?msg=testMsg444&delayTime=2000
     *
     * @param msg
     * @param delayTime
     */
    @RequestMapping("sendImporveCommonDelaymsg")
    public void sendImproveCommonMsg(String msg, Integer delayTime) {
        log.info("当前时间：{},收到请求，msg:{},delayTime:{}", new Date(), msg, delayTime);
        improveCommonMessageSender.sendDelayMsg(msg, delayTime);
    }
}

